package com.BEASTMODE.libs;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class GamePanel extends GLSurfaceView implements SurfaceHolder.Callback
{
	Activity _owner;
	GameThread _game;
	OpenGL _renderer;
	
	public GamePanel(Activity context, GameThread gt) 
	{
		super(context);
		_owner = context;
		_game = gt;
		OpenGL.Initialize(this);
		_renderer = OpenGL.GetInstance();
		setEGLContextClientVersion(2);
		setRenderer(_renderer);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) 
	{
		super.surfaceCreated(holder);
		_game.setRunning(true);
		_game.start();
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
	{
		super.surfaceChanged(holder, format, width, height);
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{
		super.surfaceDestroyed(holder);
		_game.setRunning(false);
		try 
		{
			_game.join();
		} 
		catch (InterruptedException e) 
		{
			e.printStackTrace();
		}
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent evt)
	{
		boolean retval = super.onTouchEvent(evt);
		_game.TouchEventReceived(evt);
		return retval;
	}
}
